package com.zhuoyue.web.controller;

import com.zhuoyue.biz.core.api.service.impl.EavizVerificationcodeService;
import com.zhuoyue.common.aop.auth.CheckLogin;

import com.zhuoyue.common.aop.auth.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zhuoyue.biz.core.api.domain.EavizVerificationcodeDO;
import com.zhuoyue.common.dto.ControllerResult;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * @ClassName: AppEzVerificationCodeController
 * @Description:摄像头验证
 * @author wjming
 * @Date 2021/2/14 19:41
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/viewAuthcode")
@Validated
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))

public class AppEzVerificationCodeController {

    private final EavizVerificationcodeService eavizVerificationcodeService;

    /**
     * 查询验证码
     *
     * @param serialNum 序列号
     * @return
     */

    @GetMapping("/getCode")
    @CheckLogin
    public ControllerResult getEzVerificationCode(@RequestParam @NotBlank(message = "序列号不能为空") String serialNum, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [getEzVerificationCode] -> [{}]", user.getAccount(), user.getOrgCode(), serialNum);
        EavizVerificationcodeDO number = new EavizVerificationcodeDO();
        number.setSerialNum(serialNum);
        String getNum = eavizVerificationcodeService.getVerifiCationCode(number);
        if (StringUtils.isEmpty(getNum)) {
            return ControllerResult.toNoDataFail("查询失败");
        }
        log.info("result -> [getEzVerificationCode] -> [{}]", getNum.trim());
        return ControllerResult.toReturnDataSuccess("成功", getNum.trim());
    }

    /**
     * 此处加上homeId的目的是当删除家庭时这边也需要一起删掉
     * 修改和添加并用
     *
     * @param homeId
     * @param serialNum        设备序列号
     * @param verificationCode 验证码
     * @return
     */
    @PostMapping("/updateCode")
    @CheckLogin
    public ControllerResult saveOrUpdateEzVerificationCode(@NotBlank(message = "家庭Id不能为空！") String homeId,
                                         @NotBlank(message = "设备序列号不能为空!") String serialNum,
                                         @NotBlank(message = "验证码不能为空!") String verificationCode, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [saveOrUpdateEzVerificationCode] -> [homeId:{},serialNum:{},verificationCode:{}]", user.getAccount(), user.getOrgCode(), homeId, serialNum, verificationCode);
        EavizVerificationcodeDO mesObj = new EavizVerificationcodeDO();
        mesObj.setSerialNum(serialNum);
        mesObj.setVerificationCode(verificationCode);
        mesObj.setHomeId(homeId);
        eavizVerificationcodeService.updateVerifiCationCode(mesObj);
        return ControllerResult.toReturnMessageSuccess("操作成功");
    }


    /**
     * 删除摄像头
     *
     * @param serialNum 设备序列号
     * @return
     */
    @DeleteMapping("/delCode")
    @CheckLogin
    public ControllerResult delEzDeviceMessage(@NotBlank(message = "设备序列号不能为空！") String serialNum, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [delEzDeviceMessage] -> [serialNum:{}]", user.getAccount(), user.getOrgCode(), serialNum);
        eavizVerificationcodeService.remove(serialNum);
        return ControllerResult.toReturnMessageSuccess("删除成功");
    }


}



